# -*- coding: utf-8 -*-
'''
version 2.0
__author__: YB.
contact yb80102@126.com
'''

import xlrd, os
#global variable
version = '<version><![CDATA[1]]></version>\n'
internalid = 50
##########################
def format_content(s):
    slist = s.split('\n')
    slist = ['<p>'+i+'</p>' for i in slist]
    return '\n'.join(slist)
def write_titile(f):
    f.write('<?xml version="1.0" encoding="UTF-8"?>\n')
def write_tag(f, tag, content):
    f.write('    <%s><![CDATA[\n    %s\n    ]]></%s>\n'%(tag, content, tag))
def write_nodeorder(f, order):
    f.write('<node_order><![CDATA[%s]]></node_order>\n'%order)
def write_testsuite(f, excelbook):
    testsuitename = excelbook.sheets()[0].cell(0, 1).value.encode('utf8')
    testsuitedetail = excelbook.sheets()[0].cell(1, 1).value.encode('utf8')
    #testsuiteid = excelbook.sheets()[0].cell(2, 1).value
    testsuiteid = 1
    f.write('<testsuite name="%s" >'%testsuitename)
    write_nodeorder(f, testsuiteid)
    write_tag(f, 'details', format_content(testsuitedetail))
    write_testmodule(f, excelbook)
    f.write('</testsuite>\n')
def write_testmodule(f, excelbook):
    global internalid
    for i in range(1, excelbook.nsheets):
        book = excelbook.sheet_by_index(i)
        modulename = book.cell(0, 1).value.encode('utf8')
        moduleindex = i-1
        moduledetial = book.cell(1, 1).value.encode('utf8')
        f.write('<testsuite name="%s" >\n\
        <node_order><![CDATA[%d]]></node_order>\n\
            <details><![CDATA[\n\
            %s\n\
            ]]></details>\n'%(modulename, moduleindex, format_content(moduledetial)))
        actions = []
        expections = []
        names = []
        importances = []
        summarys = []
        preconditions = []
        for i in range(3, book.nrows):
            actions.append(book.cell(i, 4).value.encode('utf8'))
            expections.append(book.cell(i, 5).value.encode('utf8'))
            names.append(book.cell(i, 0).value.encode('utf8'))
            importances.append(book.cell(i, 1).value)
            summarys.append(book.cell(i, 2).value.encode('utf8'))
            preconditions.append(book.cell(i, 3).value.encode('utf8'))
        nodeorder = 100
        for i in range(0, len(names)):
            write_testnode(f, nodeorder, names[i], importances[i], summarys[i], preconditions[i], actions[i], expections[i])
            nodeorder += 1
            internalid += 2
        f.write('</testsuite>\n')
        internalid += 10
def write_testnode(f, nodeorder, nodename, nodeimportance, nodesummary, nodeprecondition, action, expection):
    global version, internalid
    f.write('<testcase internalid="%d" name="%s">\n'%(internalid, nodename))
    write_nodeorder(f, nodeorder)
    f.write(version)
    write_tag(f, 'summary', format_content(nodesummary))
    write_tag(f, 'preconditions', format_content(nodeprecondition))
    if not nodeimportance:
        nodeimportance = '2'
    f.write('    <execution_type><![CDATA[1]]></execution_type>\n\
    <importance><![CDATA[%s]]></importance>\n\
    <estimated_exec_duration></estimated_exec_duration>\n\
    <status>1</status>\n'%nodeimportance)
    write_steps(f, action, expection)
    f.write('</testcase>\n')
def write_steps(f, action, expection):
    if action.find('----split line----')==-1:
        f.write('<steps>\n<step>\n    <step_number><![CDATA[1]]></step_number>\n')
        act = format_content(action)
        expect = format_content(expection)
        write_tag(f, 'actions', act)
        write_tag(f, 'expectedresults', expect)
        f.write('    <execution_type><![CDATA[1]]></execution_type>\n')
        f.write('</step>\n</steps>\n')
    else:
        acts = action.split('----split line----')
        expects = expection.split('----split line----')
        f.write('<steps>\n')
        for i in range(0, len(acts)):
            f.write('<step>\n    <step_number><![CDATA[%d]]></step_number>\n'%(i+1))
            act = format_content(acts[i])
            expect = format_content(expects[i])
            write_tag(f, 'actions', act)
            write_tag(f, 'expectedresults', expect)
            f.write('    <execution_type><![CDATA[1]]></execution_type>\n')
            f.write('</step>\n')
        f.write('</steps>\n')

def convert2xml(excelfile):
    if 'resultOFxml' not in os.listdir('./'):
        os.mkdir(r'./resultOFxml/')
    resfile = open('./resultOFxml/' + excelfile.split('.')[0] + '.xml', 'w')
    excelfl = xlrd.open_workbook(excelfile)
    write_titile(resfile)
    write_testsuite(resfile, excelfl)
    resfile.close()
    print '转换完毕，xml文件保存在resultOFxml文件夹中！'.decode('utf8').encode('gbk')


if __name__=="__main__":
    count = 0
    for i in os.listdir('./'):
        if i.find('.xls')!=-1:
            excelfilename = i
            convert2xml(excelfilename)
            count += 1
    print ('共转换文件%d个'%count).decode('utf8').encode('gbk')
    raw_input('press ENTER to exit')





